package koordinator;

import ggt.Koordinator;
import ggt.KoordinatorHelper;

import monitor.Monitor;
import monitor.MonitorHelper;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;


public class KoordinatorServer {
	
	public static void main(String[] args) {
        try {
            // ORB Eigenschaften setzen
            ORB orb = ORB.init(args, null);

            // Referenz von rootPOA holen und POA Manager aktivieren
            POA rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
            rootPoa.the_POAManager().activate();

            // Referenz zum Namensdiesnt (root naming context) holen
            NamingContextExt nameservice = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
            
            //Referenz vom Monitor vom Namensdienst holen
            org.omg.CORBA.Object obj = nameservice.resolve_str(args[5]);
            Monitor monitor = MonitorHelper.narrow(obj);
            
            KoordinatorImpl koor = new KoordinatorImpl(orb, monitor);
			org.omg.CORBA.Object ref = rootPoa.servant_to_reference(koor);
			Koordinator href = KoordinatorHelper.narrow(ref);
			
            //Koordinator beim Namensdienst registieren
            NameComponent path[] = nameservice.to_name(args[4]);
            nameservice.rebind(path, href);           
           
            System.out.printf("Koordinator %s bereit\n", args[4]);
            orb.run();

        } catch (Exception e) {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
        }
        System.out.printf("Koordinator %s wurde beendet\n", args[4]);
    }
}
